/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.front.product;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.news.NewsService;
import shop.services.front.news.bean.News;
import shop.services.front.plan.PlanService;
import shop.services.front.plan.bean.Plan;
import shop.services.front.product.ProductService;
import shop.services.front.product.bean.Product;
import shop.services.front.productcategory.ProductcategoryService;
import shop.services.front.productcategory.bean.Productcategory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @类名称：ProductController
 * @创建人：Wzl
 * @创建时间：2018-05-09 下午19:24:05      
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/front/product")
public class ProductController extends FrontBaseController<Product> {
    @Resource
    private ProductService productService;
    @Resource
    private ProductcategoryService productcategoryService;
    @Resource
    private PlanService planService;
    @Resource
    private NewsService newsService;

    @Override
    public Services<Product> getService() {
        return productService;
    }

    /**
     * 查询全部产品列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectProductList")
    @ResponseBody
    public JSONResult selectProductList(Productcategory productcategory) {
        List<Productcategory> categoryList = productcategoryService.selectList(productcategory);
        if (categoryList != null && categoryList.size() > 0) {
            for (Productcategory pc : categoryList) {
                Product product = new Product();
                product.setCategoryID(pc.getId());
                List<Product> productList = productService.selectList(product);
                pc.setProductList(productList);
            }
        }
        jsonResult = new JSONResult();
        jsonResult.setData(categoryList);
        return jsonResult;
    }

    /**
     * 查询全部搜索列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectAllSearchList")
    @ResponseBody
    public JSONResult selectAllSearchList(String name) {
        List<Object> searchList = new ArrayList<>();
        Product product = new Product();
        product.setName(name);
        List<Product> productList = productService.selectAllSearchList(product);
        if (productList != null && productList.size() > 0)
            searchList.addAll(productList);
        Plan plan = new Plan();
        plan.setName(name);
        List<Plan> planList = planService.selectAllSearchList(plan);
        if (planList != null && planList.size() > 0)
            searchList.addAll(planList);
        News news = new News();
        news.setName(name);
        List<News> newsList = newsService.selectAllSearchList(news);
        if (newsList != null && newsList.size() > 0)
            searchList.addAll(newsList);
        jsonResult = new JSONResult();
        jsonResult.setData(searchList);
        return jsonResult;
    }
}
